package com.aiss.ws.adapter;

import android.content.Context;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.aiss.ws.Entity.CurseItem;
import com.aiss.ws.Entity.TypeItem;
import com.aiss.ws.R;

import java.util.List;

/**
 * Created by chenqiang
 */
public class CourseDetailAdapter extends BaseAdapter {

    private List<TypeItem> datas;
    private Context context;
    private Handler handler;

    public CourseDetailAdapter(List<TypeItem> datas, Context context,Handler handler) {
        this.handler = handler;
        this.datas = datas;
        this.context = context;
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        Holder holder;
        if(convertView==null){
            holder = new Holder();
            convertView = LayoutInflater.from(context).inflate(R.layout.course_detail_item, null);
            holder.rel = (RelativeLayout) convertView.findViewById(R.id.rel);
            holder.img = (ImageView) convertView.findViewById(R.id.img);
            holder.name = (TextView) convertView.findViewById(R.id.name);
            convertView.setTag(holder);
        }else{
            holder = (Holder) convertView.getTag();
        }

        holder.name.setText(datas.get(position).getName());

        if(1==(datas.get(position).getChange())){
            holder.img.setImageResource(R.mipmap.iocn_2x12);
        }else{
            holder.img.setImageResource(R.mipmap.iocn_2x13);
        }

        holder.rel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for(int i = 0 ; i < datas.size();i++){
                    if(position==i){
                        datas.get(i).setChange(1);
                    }else{
                        datas.get(i).setChange(0);
                    }
                }
                notifyDataSetChanged();
                handler.sendEmptyMessage(1);
            }
        });



        return convertView;
    }

    class Holder{
        TextView name;
        RelativeLayout rel;
        ImageView img;
    }

}
